package com.wyp.didishige.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;

import com.wyp.didishige.db.dao.BlackUserDao;
import com.wyp.didishige.db.dao.DaoMaster;
import com.wyp.didishige.db.dao.DaoSession;
import com.wyp.didishige.model.User;

import java.util.HashMap;
import java.util.List;

import cn.bmob.v3.BmobUser;


/**
 * 黑名单管理
 */
@SuppressWarnings("ALL")
public class BlackUserManager {

    private DaoMaster.DevOpenHelper openHelper;
    Context mContecxt;
    String uid = null;
    private static HashMap<String, BlackUserManager> daoMap = new HashMap<>();

    /**
     * 获取DB实例
     *
     * @param context
     * @return
     */
    public static BlackUserManager getInstance(Context context) {
        User user = BmobUser.getCurrentUser(context, User.class);
        String loginId = user.getObjectId();
        if (TextUtils.isEmpty(loginId)) {
            throw new RuntimeException("you must login.");
        }
        BlackUserManager dao = daoMap.get(loginId);
        if (dao == null) {
            dao = new BlackUserManager(context, loginId);
            daoMap.put(loginId, dao);
        }
        return dao;
    }

    private BlackUserManager(Context context, String uId) {
        clear();
        this.mContecxt = context.getApplicationContext();
        this.uid = uId;
        String DBName = uId + ".demodb";
        this.openHelper = new DaoMaster.DevOpenHelper(mContecxt, DBName, null);
    }

    /**
     * 清空资源
     */
    public void clear() {
        if (openHelper != null) {
            openHelper.close();
            openHelper = null;
            mContecxt = null;
            uid = null;
        }
    }

    private DaoSession openReadableDb() {
        checkInit();
        SQLiteDatabase db = openHelper.getReadableDatabase();
        DaoMaster daoMaster = new DaoMaster(db);
        DaoSession daoSession = daoMaster.newSession();
        return daoSession;
    }

    private DaoSession openWritableDb() {
        checkInit();
        SQLiteDatabase db = openHelper.getWritableDatabase();
        DaoMaster daoMaster = new DaoMaster(db);
        DaoSession daoSession = daoMaster.newSession();
        return daoSession;
    }

    private void checkInit() {
        if (openHelper == null) {
            throw new RuntimeException("请初始化db");
        }
    }

    //-------------------------------------------------------------

    /**
     * 获取本地所有黑名单用户
     *
     * @return
     */
    public List<BlackUser> getAllBlackUser() {
        BlackUserDao dao = openReadableDb().getBlackUserDao();
        return dao.queryBuilder().orderDesc(BlackUserDao.Properties.Id).list();
    }

    /**
     * 创建或更新新黑名单
     *
     * @param blackUser
     * @return long:返回插入或修改的id
     */
    public long insertOrUpdateBlackUser(BlackUser blackUser) {
        BlackUserDao dao = openWritableDb().getBlackUserDao();
        BlackUser local = getBlackUser(blackUser.getUid());
        if (local == null) {
            return dao.insertOrReplace(blackUser);
        } else {
            return -1;
        }
    }

    /**
     * 根据指定的uid获取本地的黑名单
     *
     * @param uid
     * @return
     */
    public BlackUser getBlackUser(String uid) {
        BlackUserDao dao = openReadableDb().getBlackUserDao();
        return dao.queryBuilder().where(BlackUserDao.Properties.Uid.eq(uid)).build().unique();
    }

    /**
     * 批量插入黑名单
     *
     * @param blackUsers
     */
    public void insertBatchBlackUser(List<BlackUser> blackUsers) {
        BlackUserDao dao = openWritableDb().getBlackUserDao();
        dao.insertOrReplaceInTx(blackUsers);
    }

    /**
     * 删除指定的黑名单
     *
     * @param blackUser
     */
    public void deleteBlackUser(BlackUser blackUser) {
        BlackUserDao dao = openWritableDb().getBlackUserDao();
        dao.delete(blackUser);
    }

}
